<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * CI-Merchant Library
 *
 * Copyright (c) 2011-2012 Adrian Macneil
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * Merchant 2Checkout Class
 *
 * Payment processing using 2Checkout
 * Documentation: http://www.2checkout.com/documentation/Advanced_User_Guide.pdf
 */

class Merchant_paypal_standard extends Merchant_driver
{
	const PROCESS_URL = 'https://www.paypal.com/webscr';
	const PROCESS_URL_TEST = 'https://www.sandbox.paypal.com/webscr';
	
	public function default_settings()
	{
		return array(
			'paypal_email' => '',
			'test_mode' => FALSE
		);
	}

	public function purchase()
	{
		$this->require_params('amount', 'currency');
		//echo "Hello";
		$data =array(
			'cmd'=>'_xclick',
			'amount' => $this->param('amount'),
			'item_name' => $this->param('item_name'),
			'item_number' => $this->param('item_number'),
			'upload'=>1,
			'business'=>$this->param('business'),
			'quantity'=>1,
			'email' => $this->param('email'),
			'first_name'=>$this->param('first_name'),
			'last_name'=>$this->param('last_name'),
			'address1'=>$this->param('address1'),
			'city'=>$this->param('city'),
			'zip'=>$this->param('zip'),
			'country'=>$this->param('country'),
			'address_override'=>'0',
			'cbt' => "Return to Alan",
			'no_shipping' => '1',
			'no_note' => '1',
			'currency_code' => 'USD',
			
			'charset' => 'UTF-8',
			'custom' => $this->param('custom'),
			'rm' => '2',
			'return' => $this->param('return'),
			'notify_url' => $this->param('notify_url'),
			'cancel_return'=>$this->param('cancel_return'),
			'cbt' => 'Alan Upgrade Package'
		);
	//print_r($data);
		if ($this->setting('test_mode'))
		{
			$this->redirect(self::PROCESS_URL_TEST.'?'.http_build_query($data));
		}
		else
		{
			$this->redirect(self::PROCESS_URL.'?'.http_build_query($data));
		}

		
	}

	public function purchase_return()
	{
		$order_number = $this->CI->input->get_post('order_number');

		// strange exception specified by 2Checkout
		if ($this->setting('test_mode'))
		{
			$order_number = '1';
		}

		$key = strtoupper(md5($this->setting('secret_word').$this->setting('account_no').$order_number.$this->amount_dollars()));
		if ($key == $this->CI->input->get_post('key'))
		{
			return new Merchant_response(Merchant_response::COMPLETE, NULL, $order_number);
		}

		return new Merchant_response(Merchant_response::FAILED, lang('merchant_invalid_response'));
	}
}

/* End of file ./libraries/merchant/drivers/merchant_paypal.php */